package com.pinxixi.specification.service;

import com.pinxixi.framework.domain.specification.Specification;
import com.pinxixi.framework.domain.specification.SpecificationOption;
import com.pinxixi.framework.domain.specification.ext.SpecificationExt;
import com.pinxixi.framework.domain.specification.ext.SpecificationEntity;
import com.pinxixi.framework.domain.specification.response.SpecificationCode;
import com.pinxixi.framework.model.response.*;
import com.pinxixi.specification.mapper.SpecificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecificationService {

    @Autowired
    private SpecificationMapper specificationMapper;

    public ListResponseResult<Specification> findAll() {
        List<Specification> list = specificationMapper.findAll();
        QueryResult<Specification> queryResult = new QueryResult<>();
        queryResult.setList(list);
        return new ListResponseResult<Specification>(CommonCode.SUCCESS,queryResult);
    }

    public ListResponseResult<Specification> findPage(Integer page, Integer size, Specification specification) {
        //limit 起始条的索引，每页条数
        Integer index = size*(page-1);
        List<SpecificationExt> list = specificationMapper.findPage(index,size, specification.getSpecName());
        Integer count = specificationMapper.selectCount(specification.getSpecName());
        QueryResult<SpecificationExt> queryResult = new QueryResult<SpecificationExt>();
        queryResult.setList(list);
        queryResult.setTotal(count);
        return new ListResponseResult<Specification>(CommonCode.SUCCESS,queryResult);
    }

    public QueryResponseResult<SpecificationEntity> findOne(Integer id) {
        //根据ID查询规格信息
        Specification specification = specificationMapper.findOne(id);
        if(specification==null){
            //规格不存在
            return new QueryResponseResult(SpecificationCode.SPECIFICATION_NOT_EXISTS,null);
        }
        //根据ID查询规格参数信息
        List<SpecificationOption> list = specificationMapper.findOptionsById(id);
        //将规格信息和规格参数信息合并
        SpecificationEntity entity = new SpecificationEntity();
        entity.setOptionList(list);
        entity.setSpecification(specification);
        //返回给前端
        return new QueryResponseResult<>(CommonCode.SUCCESS,entity);
    }

    public ResponseResult save(SpecificationEntity entity) {
        Specification specification = entity.getSpecification();
        if(specification.getId()!=null){
            //执行修改
            //修改规格表
            specificationMapper.editSpecification(specification);
            List<SpecificationOption> optionList = entity.getOptionList();
            //为了不把新增的规格参数删除
            //先删除不要的旧数据（数据库中已经存在的数据）
            specificationMapper.deleteOptions(entity);
            for(SpecificationOption option: optionList){
                if(option.getId()!=null){
                    //修改已有参数
                    specificationMapper.editOption(option);
                }else{
                    //新增参数
                    option.setSpecId(specification.getId());
                    specificationMapper.addOption(option);
                }
            }
        }else{
            //1.对规格名称入库。得到规格ID
            specificationMapper.add(entity.getSpecification());
            //2.对前端传递过来的参数集合做遍历
            //每遍历一次进行一次新增
            for (SpecificationOption option: entity.getOptionList()) {
                option.setSpecId(specification.getId());
                specificationMapper.addOption(option);
            }
        }
        return new ResponseResult(CommonCode.SUCCESS);
    }

    public ResponseResult delete(Integer id) {
        specificationMapper.deleteOptionsBySpecId(id);
        specificationMapper.deleteSpecification(id);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    public ResponseResult deleteAll(Integer[] ids) {
        for (Integer id:ids) {
            specificationMapper.deleteOptionsBySpecId(id);
            specificationMapper.deleteSpecification(id);
        }
        return new ResponseResult(CommonCode.SUCCESS);
    }

    public ListResponseResult<SpecificationOption> findOptionsBySpecId(Integer id) {
        List<SpecificationOption> list = specificationMapper.findOptionsById(id);
        QueryResult<SpecificationOption> queryResult = new QueryResult<>();
        queryResult.setList(list);
        return new ListResponseResult<SpecificationOption>(CommonCode.SUCCESS,queryResult);
    }
}
